package com.lefevre.cms.web.action.like;

import com.lefevre.cms.bean.like.Like;
import com.lefevre.cms.bean.like.TopicLike;
import com.lefevre.cms.service.like.LikeService;
import com.lefevre.cms.utils.UUIDUtil;
import com.lefevre.cms.utils.Verification;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 点赞管理
 */
@Component("likeManage")
public class LikeManage {

    @Resource
    LikeService likeService;

    /**
     * 取得点赞Id的用户Id(后N位)
     *
     * @param likeId 点赞Id
     * @return
     */
    public int getLikeId(String likeId) {
        String after_userId = likeId.substring(likeId.length() - 4, likeId.length());
        return Integer.parseInt(after_userId);
    }

    /**
     * 生成点赞Id
     * 点赞Id由32位uuid+1位用户Id后4位组成
     *
     * @param userId 用户Id
     * @return
     */
    public String createLikeId(Long userId) {
        //选取得后N位用户Id
        String afterUserId = String.format("%04d", userId % 10000);
        return UUIDUtil.getUUID32() + afterUserId;
    }

    /**
     * 校验点赞Id
     * 点赞Id要先判断是否36位并且最后4位是数字
     *
     * @param likeId 点赞Id
     * @return
     */
    public boolean verificationLikeId(String likeId) {
        if (likeId != null && !"".equals(likeId.trim())) {
            if (likeId.length() == 36) {
                String after_userId = likeId.substring(likeId.length() - 4, likeId.length());
                boolean verification = Verification.isPositiveIntegerZero(after_userId);//数字
                if (verification) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 生成点赞对象
     *
     * @return
     */
    public Object createLikeObject(Like like) {
        return like;
    }


    /**---------------------------------------------- 话题点赞 ----------------------------------------------**/
    /**
     * 取得话题点赞Id的话题Id(后N位)
     *
     * @param topicLikeId 话题点赞Id
     * @return
     */
    public int getTopicLikeId(String topicLikeId) {
        String[] idGroup = topicLikeId.split("_");
        long topicId = Long.parseLong(idGroup[0]);

        //选取得后N位话题Id
        String after_topicId = String.format("%04d", Math.abs(topicId) % 10000);
        return Integer.parseInt(after_topicId);
    }

    /**
     * 生成话题点赞Id
     * 话题点赞Id格式（话题Id_用户Id）
     *
     * @param topicId 话题Id
     * @param userId  用户Id
     * @return
     */
    public String createTopicLikeId(Long topicId, Long userId) {
        return topicId + "_" + userId;
    }

    /**
     * 校验话题点赞Id
     * 话题点赞Id要先判断最后4位是不是数字
     *
     * @param topicLikeId 话题点赞Id
     * @return
     */
    public boolean verificationTopicLikeId(String topicLikeId) {
        if (topicLikeId != null && !"".equals(topicLikeId.trim())) {
            String[] idGroup = topicLikeId.split("_");
            for (String id : idGroup) {
                boolean verification = Verification.isPositiveIntegerZero(id);//数字
                if (!verification) {
                    return false;
                }
                return true;
            }

        }
        return false;
    }


    /**
     * 生成话题点赞对象
     *
     * @return
     */
    public Object createTopicLikeObject(TopicLike topicLike) {
        return topicLike;
    }


    /**
     * 查询缓存 查询话题点赞
     *
     * @param topicLikeId 话题点赞Id
     * @return
     */
    @Cacheable(value = "likeManage_cache_findTopicLikeById", key = "#topicLikeId")
    public TopicLike query_cache_findTopicLikeById(String topicLikeId) {
        return likeService.findTopicLikeById(topicLikeId);
    }

    /**
     * 删除缓存 话题点赞
     *
     * @param topicLikeId 话题点赞Id
     * @return
     */
    @CacheEvict(value = "likeManage_cache_findTopicLikeById", key = "#topicLikeId")
    public void delete_cache_findTopicLikeById(String topicLikeId) {
    }


    /**
     * 查询缓存 根据话题Id查询被点赞数量
     *
     * @param topicId 话题Id
     * @return
     */
    @Cacheable(value = "likeManage_cache_findLikeCountByTopicId", key = "#topicId")
    public Long query_cache_findLikeCountByTopicId(Long topicId) {
        return likeService.findLikeCountByTopicId(topicId);
    }

    /**
     * 删除缓存 根据话题Id查询被点赞数量
     *
     * @param topicId 话题Id
     * @return
     */
    @CacheEvict(value = "likeManage_cache_findLikeCountByTopicId", key = "#topicId")
    public void delete_cache_findLikeCountByTopicId(Long topicId) {
    }

}
